//
//  main.swift
//  SetAndMap
//
//  Created by lhc on 2024/6/22.
//

import Foundation

/// 层序遍历输出二分搜索树内容
func levelOrderBST<E: Comparable>(b: BinarySearchTree<E>) {
    var levelOrderResults = [E]()
    b.levelOrder { e in
        levelOrderResults.append(e!)
    }
    print(levelOrderResults)

}

func removeElement<E: Comparable>(b: BinarySearchTree<E>, e: E) {
    print("删除元素\(e)")
    do {
        
        try b.removeElement(e)
        
    }catch BinarySearchTree<Int>.Exception.emptyTree {
        print("请不要在空树里删除元素")
    }catch BinarySearchTree<Int>.Exception.notContainElement(let e) {
        print("树中没有元素\(e)")
    }catch {
        print("未知错误")
    }
}

func nameEqualsMain() {
    let l1 = [8, 4, 2, 9, 6, 3, 5, 7, 1]
        //          8
        //      4       9
        //   2     6
        // 1   3  5   7

    let b1 = BinarySearchTree<Int>()
    for e in l1 {
        b1.add(e: e)
    }

    levelOrderBST(b: b1)


    removeElement(b: b1, e: 10)
    removeElement(b: b1, e: 4)

    levelOrderBST(b: b1)
}


//nameEqualsMain()

let map1 = BinarySearchTreeMap<String, Int>()
map1.setValueBy(key: "11", value: 11)
map1.setValueBy(key: "14", value: 14)
map1.setValueBy(key: "10", value: 10)
map1.setValueBy(key: "12", value: 12)
print(map1)
print(map1.getValueBy(key: "10"))
print(map1.getValueBy(key: "11"))
map1.remove(key: "13")
print(map1.getValueBy(key: "13"))
print(map1)








